<script type='text/javascript'>
var saved = true;
function check_saved() {
//	if (!saved)
//		return confirm("You modified some information in the applicant list which are not yet saved. If you continue these modifications will be lost. Are you sure you want to continue ?");
	return true;
}
var changed_fields = new Array();
function field_change(name, row, special) {
	saved = false;
	var found = false;
	for (var i = 0; i < changed_fields.length; ++i) if (changed_fields[i] == name) { found = true; break; }
	if (!found) changed_fields.push(name);
	var button = document.all ? document.all["save_button1"] : document.getElementById("save_button1");
	button.disabled = "";
	button = document.all ? document.all["save_button2"] : document.getElementById("save_button2");
	button.disabled = "";
	window.onbeforeunload = check_before_to_close;
	if (special == 'bool') {
		var check = document.forms["applicants_table"].elements["bool__"+name].checked;
		document.forms["applicants_table"].elements[name].value = check ? 1 : 0;
	}
	for (var linked_field in linked_fields) {
		if (name.indexOf(linked_field)==0) {
			for (var span_id in linked_fields[linked_field]) {
				var values = linked_fields[linked_field][span_id];
				var value = document.forms["applicants_table"].elements[name].value;
				var linked_value;
				if (value == "NULL") linked_value = "";
				else {
					var linked_value = values[value];
					if (linked_value == null) { alert("Linked value error: nothing linked with value "+value); continue; }
				}
				var span = document.all ? document.all["link__field__"+span_id+"__"+row] : document.getElementById("link__field__"+span_id+"__"+row);
				span.innerHTML = linked_value;
			}
		}
	}
}
function prepare_save() {
	var form = document.forms["applicants_table"];
	for (var i = 0; i < form.elements.length; ++i) {
		var e = form.elements[i];
		if (e.name == "from_save") continue;
		if (e.name == "timestamp") continue;
		if (e.name == "save_button") continue;
		var found = false;
		for (var j = 0; j < changed_fields.length; ++j)
			if (changed_fields[j] == e.name) { found = true; break; }
		if (!found) { e.parentNode.removeChild(e); e.name = 'nochange'; i--; }
	}
	var e = document.createElement('DIV');
	e.id = 'from_save_button';
	e.style.visibility='hidden';
	e.style.position='absolute';
	e.style.top = '-1000px';
	document.body.appendChild(e);
	form.submit();
}
function fields_saved() {
	saved = true;
	changed_fields = new Array();
	//var button = document.all ? document.all["save_button"] : document.getElementById("save_button");
	//button.disabled = "disabled";
	window.onbeforeunload = null;
	<?php if (isset($_SESSION["debug"]) && $_SESSION["debug"]=='yes') {} else {?>
	document.forms['fields_selection'].submit();
	<?php }?>
}
function check_before_to_close(evt) {
	var event = window.event ? window.event : evt;
	event.returnValue = "You modified some information in the applicant list which are not yet saved. If you continue these modifications will be lost. Are you sure you want to continue ?";
}

var linked_fields = new Array();
<?php
require_once("app_fields.inc");
$done = array();
foreach ($app_display_names as $section_name=>$section_fields)
	foreach ($section_fields as $field=>$name) {
		$f = new DisplayField($field, $name);
		if ($f->indirection == null) continue;
		if (in_array($f->table."$".$f->field, $done)) continue;
		$key_field = null;
		$linked_fields = array();
		$passed = false;
		if (strpos($f->indirection, "#")>0) array_push($linked_fields, $f);
		foreach ($section_fields as $field2=>$name2) {
			$f2 = new DisplayField($field2, $name2);
			if ($f2->indirection == null) continue;
			if ($f->table == $f2->table && $f->field == $f2->field) {
				if ($f->indirection == $f2->indirection) {
					$passed = true;
					if (strpos($f->indirection, "#")>0) continue;
					if ($key_field == null)
						$key_field = $f;
					else
						array_push($linked_fields, $f);
					continue;
				}
				if (strpos($f2->indirection,"#")>0) {
					array_push($linked_fields, $f2);
					continue;
				}
				if ($passed) {
					if ($key_field == null)
						$key_field = $f2;
					else
						array_push($linked_fields, $f2);
					continue;
				}
				$key_field = $f2;
			}
		}
		if ($key_field == null || count($linked_fields)==0) continue;

		$type = $app_fields[$key_field->table][$key_field->field];
		$type = explode("#", $type);
		$indirect_key_table = $type[1];
		$indirect_key_field = $type[2];
		
		$select_fields = array();
		$joins = array();
		$select_fields[$indirect_key_table.".".$indirect_key_field] = "\$ID\$";
		foreach ($linked_fields as $lf) {
			$indirections = explode("#", $lf->indirection);
			if (count($indirections)==1) {
				$select_fields[$indirect_key_table.".".$lf->indirection] = $lf->table."$".$lf->field."$".str_replace("#", "$", $lf->indirection);
			} else {
				$indirect_table = $indirect_key_table;
				$i = 0;
				$indirect_table_alias = $indirect_table;
				do {
					$indirect_field = $indirections[$i];
					$type = explode("#",$app_fields[$indirect_table][$indirect_field]);
					$join_index = count($joins);
					array_push($joins, array($type[1], $type[2], $indirect_table_alias, $indirect_field));
					$indirect_table_alias = "join_table_".$join_index;
					$indirect_table = $type[1];
					$indirect_field = $type[2];
					$i++;
				} while ($i < count($indirections)-1);
				$select_fields[$indirect_table_alias.".".$indirections[$i]] = $lf->table."$".$lf->field."$".str_replace("#","$",$lf->indirection);
			}
		}
		
		$query = "SELECT ";
		$first = true;
		foreach ($select_fields as $table_field=>$alias) {
			if ($first) $first = false; else $query .= ",";
			$query .= $table_field." AS ".$alias;
		}
		$query .= " FROM ".$indirect_key_table;
		$i = 0;
		foreach ($joins as $join) {
			$query .= " LEFT JOIN ".$join[0]." AS join_table_".$i." ON join_table_".$i.".".$join[1]."=".$join[2].".".$join[3];
			$i++;
		}
		
		$result = DataBase::query($query);
//		echo "//".$query."\n//".($result === false ? "FALSE" : count($result))."\n";
		
		echo "linked_fields[\"".$key_field->table."$".$key_field->field."\"] = {";
		$first = true;
		foreach ($linked_fields as $lf) {
			if ($first) $first = false; else echo ",";
			$lf_name = $lf->table."$".$lf->field."$".str_replace("#", "$", $lf->indirection);
			echo "\"".$lf_name."\":{";
			$first2 = true;
			foreach ($result as $r) {
				if ($first2) $first2 = false; else echo ",";
				echo "'".$r["\$ID\$"]."':\"".$r[$lf_name]."\"";
			}
			echo "}";
		}
		echo "};\r\n";
		array_push($done,$f->table."$".$f->field);
	}
?>
</script>
<?php include $_SESSION["root_path"]."/field_validators.inc";?>
